﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Business;
using University.Workshop.DataAccess;
using University.Workshop.Model;

namespace University.Workshop.WebApp
{
    public partial class ViewWebmasterRequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
            if (!IsPostBack)
            {
                long requestId = -1;
                if (Request.QueryString["reqId"] != null)
                    requestId = long.Parse(Request.QueryString["reqId"].ToString());
                if (requestId != -1)
                {
                    Model.WebmasterRequest webmasterReq = WebmasterRequestDataAccess.GetWebmasterRequestById(requestId);
                    if (webmasterReq == null)
                    {
                        if (WebmasterManager.IsWebmaster(User.Identity.Name) &&
                            WebmasterDataAccess.GetWebmasterByUserId(
                                UserDataAccess.GetUserByEmail(User.Identity.Name).Id).CanSetWebmasters)
                            MultiViewRequestInfo.ActiveViewIndex = 2;
                        else
                        {
                            Response.Write("<script>if(window.history.length>1){history.back();} else {window.location.href = 'Default.aspx';}</script>");
                        }
                    }
                    else
                    {
                        if (WebmasterManager.IsWebmaster(User.Identity.Name) && WebmasterDataAccess.GetWebmasterByUserId(UserDataAccess.GetUserByEmail(User.Identity.Name).Id).CanSetWebmasters)
                        {
                            if (webmasterReq.Status == WebmasterRequestStatus.Finished)
                            {
                                MultiViewRequestInfo.ActiveViewIndex = 3;
                            }
                            else
                            {
                                string userEmail =
                                UserDataAccess.GetUserById(webmasterReq.CandidateId).ContactInformation.Email;
                                LinkUser.PostBackUrl = "UserProfile.aspx?user=" + userEmail;
                                LinkUser.Text = userEmail;
                                lblDate.Text = webmasterReq.Date.ToString();
                                txtSkills.Text = webmasterReq.Skills;
                                lblReplies.Visible = webmasterReq.WantsToReplyTickets;
                                lblWebmasters.Visible = webmasterReq.WantsToSetWebmasters;
                                lblPositives.Text = webmasterReq.PositiveVotes + " positives";
                                lblNegatives.Text = webmasterReq.NegativeVotes + " negatives";
                                lblBalance.Text = webmasterReq.GetTotalVotes >= 0 ? "( +" + webmasterReq.GetTotalVotes + " )" : "( " + webmasterReq.GetTotalVotes + " )";
                                MultiViewRequestInfo.ActiveViewIndex = 0;
                                if (WebmasterRequestDataAccess.HasVoted(User.Identity.Name, requestId))
                                {
                                    imgBtnNegative.Visible = false;
                                    imgBtnPositive.Visible = false;
                                    lblVoteInfo.Visible = true;
                                }
                                else
                                {
                                    imgBtnNegative.Visible = true;
                                    imgBtnPositive.Visible = true;
                                    lblVoteInfo.Visible = false;
                                }
                            }
                        }
                        else
                        {
                            if (WebmasterManager.IsWebmaster(User.Identity.Name))
                                MultiViewRequestInfo.ActiveViewIndex = 1;
                            else
                            {
                                Response.Write(
                                        "<script>if(window.history.length>1){history.back();} else {window.location.href = 'Default.aspx';}</script>");
                            }
                        }
                    }
                }
                else
                {
                    Response.Write("<script>if(window.history.length>1){history.back();} else {window.location.href = 'Default.aspx';}</script>");
                }
            }
            imgBtnPositive.Attributes.Add("onmouseover", "javascript:this.src='Images/PositiveVoteOn.jpg'");
            imgBtnPositive.Attributes.Add("onmouseout", "javascript:this.src='Images/PositiveVote.jpg'");
            imgBtnNegative.Attributes.Add("onmouseover", "javascript:this.src='Images/NegativeVoteOn.jpg'");
            imgBtnNegative.Attributes.Add("onmouseout", "javascript:this.src='Images/NegativeVote.jpg'");
        }

        protected void imgBtnPositive_Click(object sender, ImageClickEventArgs e)
        {
            long requestId = -1;
            if (Request.QueryString["reqId"] != null)
                    requestId = long.Parse(Request.QueryString["reqId"].ToString());
            if (requestId != -1)
            {
                Model.WebmasterRequest webmasterReq = WebmasterRequestDataAccess.UpdateRequestVotes(requestId, 1);
                WebmasterRequestDataAccess.RecordVote(User.Identity.Name, requestId);
                lblPositives.Text = webmasterReq.PositiveVotes + " positives";
                lblNegatives.Text = webmasterReq.NegativeVotes + " negatives";
                lblBalance.Text = webmasterReq.GetTotalVotes >= 0 ? "( +" + webmasterReq.GetTotalVotes + " )" : "( " + webmasterReq.GetTotalVotes + " )";
                if (webmasterReq.GetTotalVotes >= 20 || DateTime.Now >= webmasterReq.Date.AddDays(3))
                {
                    WebmasterRequestDataAccess.FinishRequest(webmasterReq.Id);
                    if (webmasterReq.GetTotalVotes >= 20)
                    {
                        WebmasterManager.RegisterWebmaster(
                            UserDataAccess.GetUserById(webmasterReq.CandidateId).ContactInformation.Email,
                            webmasterReq.WantsToReplyTickets, webmasterReq.WantsToSetWebmasters);
                    }
                    Response.Write("<script>alert('Thank you for your colaboration, now the votation has finished');window.location.href = 'VoteWebMasterRequests.aspx';</script>");
                }
                else
                {
                    //TODO hacer que se bloqueen los botones de votacion
                    Response.Write("<script>alert('Thank you for your colaboration, your vote has been realized'); window.location.href = 'ViewWebmasterRequest.aspx?reqId=" + requestId + "';</script>");
                }
            }
        }

        protected void imgBtnNegative_Click(object sender, ImageClickEventArgs e)
        {
            long requestId = -1;
            if (Request.QueryString["reqId"] != null)
                requestId = long.Parse(Request.QueryString["reqId"].ToString());
            if (requestId != -1)
            {
                Model.WebmasterRequest webmasterReq = WebmasterRequestDataAccess.UpdateRequestVotes(requestId, -1);
                WebmasterRequestDataAccess.RecordVote(User.Identity.Name, requestId);
                lblPositives.Text = webmasterReq.PositiveVotes + " positives";
                lblNegatives.Text = webmasterReq.NegativeVotes + " negatives";
                lblBalance.Text = webmasterReq.GetTotalVotes >= 0 ? "( +" + webmasterReq.GetTotalVotes + " )" : "( " + webmasterReq.GetTotalVotes + " )";
                if (webmasterReq.GetTotalVotes >= 20 || DateTime.Now >= webmasterReq.Date.AddDays(3))
                {
                    WebmasterRequestDataAccess.FinishRequest(webmasterReq.Id);
                    if (webmasterReq.GetTotalVotes >= 20)
                    {
                        WebmasterManager.RegisterWebmaster(
                            UserDataAccess.GetUserById(webmasterReq.CandidateId).ContactInformation.Email,
                            webmasterReq.WantsToReplyTickets, webmasterReq.WantsToSetWebmasters);
                    }

                    Response.Write("<script>alert('Thank you for your colaboration, now the votation has finished');window.location.href = 'VoteWebMasterRequests.aspx';</script>");
                }
                else
                {
                    //TODO hacer que se bloqueen los botones de votacion
                    Response.Write("<script>alert('Thank you for your colaboration, your vote has been realized'); window.location.href = 'ViewWebmasterRequest.aspx?reqId="+requestId+"';</script>");
                }
            }
        }
    }
}